//
//  HSRequest+UpdateFile.m
//  Wutuojia
//
//  Created by anlun on 15/7/17.
//  Copyright (c) 2015年 anLun. All rights reserved.
//

#import "HSRequest+UpdateFile.h"
#import "NSObject+MJKeyValue.h"
#import "NSString+UrlEncode.h"

@implementation HSRequest (UpdateFile)

+(void)requestApiName:(NSString *)apiName
            andParams:(NSDictionary *)params
               andImg:(NSData *)imgData
         successBlcok:(void(^)(id sucContent))successBlock
          failedBlock:(void(^)(id failContent))failedBlock
         reloginBlock:(void(^)(id reloginContent))reloginBlock{
//    NSString *urlStr = [NSString stringWithFormat:@"%@%@",
//                        GetApiByKey(@"host"),
//                        GetApiByKey(apiName)
//                        ];
    NSString *urlStr = nil;
    NSDictionary *params1=@{
                            @"body":params
                            };
    NSString *post1=[NSString stringWithFormat:@"%@",[params1 JSONString]];
    
    NSDictionary *p2=@{
                       @"dataJson":[post1 urlEncode]
                       };
    
    NSString *post2=[NSString stringWithFormat:@"dataJson=%@",[post1 urlEncode]];
    
    NSString *str=[NSString stringWithFormat:@"%@?%@",urlStr,post2];
	
	[[HSRequest sharedInstance] POST:str parameters:p2 constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
		//添加要上传的文件，此处为图片
		[formData appendPartWithFileData:imgData
									name:@"name"
								fileName:@"file.jpeg"
								mimeType:@"image/jpeg"];
	} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		if ([responseObject[@"code"] integerValue] ==-1){
			if(failedBlock){
				failedBlock(responseObject);
			}
		}else{
			if (successBlock) {
				successBlock(responseObject);
			}
		}
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		if (failedBlock) {
			failedBlock(error);
		}
	}];
}
@end
